/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.util.List;

import com.jeesite.modules.st.utils.BillUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StSendNoticeBill;
import com.jeesite.modules.st.dao.StSendNoticeBillDao;
import com.jeesite.modules.st.entity.StSendNoticeBillDetail;
import com.jeesite.modules.st.dao.StSendNoticeBillDetailDao;

/**
 * 送货通知Service
 *
 * @author HOHD
 * @version 2020-12-19
 */
@Service
@Transactional(readOnly = true)
public class StSendNoticeBillService extends CrudService<StSendNoticeBillDao, StSendNoticeBill> {

    @Autowired
    private StSendNoticeBillDetailDao stSendNoticeBillDetailDao;

    /**
     * 获取单条数据
     *
     * @param stSendNoticeBill
     * @return
     */
    @Override
    public StSendNoticeBill get(StSendNoticeBill stSendNoticeBill) {
        StSendNoticeBill entity = super.get(stSendNoticeBill);
        if (entity != null) {
            StSendNoticeBillDetail stSendNoticeBillDetail = new StSendNoticeBillDetail(entity);
            stSendNoticeBillDetail.setStatus(StSendNoticeBillDetail.STATUS_NORMAL);
            entity.setStSendNoticeBillDetailList(stSendNoticeBillDetailDao.findList(stSendNoticeBillDetail));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param stSendNoticeBill 查询条件
     * @return
     */
    @Override
    public Page<StSendNoticeBill> findPage(StSendNoticeBill stSendNoticeBill) {
        return super.findPage(stSendNoticeBill);
    }

    public List<StSendNoticeBillDetail> findDetail(StSendNoticeBillDetail stSendNoticeBillDetail) {
        return stSendNoticeBillDetailDao.findList(stSendNoticeBillDetail);
    }

    @Autowired
    BillUtil billUtil;

    /**
     * 保存数据（插入或更新）
     *
     * @param stSendNoticeBill
     */
    @Override
    @Transactional(readOnly = false)
    public void save(StSendNoticeBill stSendNoticeBill) {
        if (stSendNoticeBill.getIsNewRecord()) {
            String billNo = billUtil.billNo(BillUtil.BillType.sendNoticeBill);//采购入库
            stSendNoticeBill.setBillNo(billNo);
        }
        super.save(stSendNoticeBill);
        // 保存 StSendNoticeBill子表
        for (StSendNoticeBillDetail stSendNoticeBillDetail : stSendNoticeBill.getStSendNoticeBillDetailList()) {
            if (!StSendNoticeBillDetail.STATUS_DELETE.equals(stSendNoticeBillDetail.getStatus())) {
                stSendNoticeBillDetail.setParentId(stSendNoticeBill);
                if (stSendNoticeBillDetail.getIsNewRecord()) {
                    stSendNoticeBillDetailDao.insert(stSendNoticeBillDetail);
                } else {
                    stSendNoticeBillDetailDao.update(stSendNoticeBillDetail);
                }
            } else {
                stSendNoticeBillDetailDao.delete(stSendNoticeBillDetail);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param stSendNoticeBill
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(StSendNoticeBill stSendNoticeBill) {
        super.updateStatus(stSendNoticeBill);
    }

    /**
     * 删除数据
     *
     * @param stSendNoticeBill
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(StSendNoticeBill stSendNoticeBill) {
        super.delete(stSendNoticeBill);
        StSendNoticeBillDetail stSendNoticeBillDetail = new StSendNoticeBillDetail();
        stSendNoticeBillDetail.setParentId(stSendNoticeBill);
        stSendNoticeBillDetailDao.deleteByEntity(stSendNoticeBillDetail);
    }

}